<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增访问信息')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-visitor-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">访问人姓名：</label>
                    <div class="col-sm-8">
                        <input name="visitorName" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">访问人电话：</label>
                    <div class="col-sm-8">
                        <input name="visitorPhone" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">访问原因：</label>
                    <div class="col-sm-8">
                        <input name="visitReason" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">访问时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="visitTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">离开时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="visitLeaveTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">访问老人：</label>
                    <div class="col-sm-8">
                        <select name="oldManId" id="oldManSelect" class="form-control" required>
                            <!-- 分类选项将通过 JavaScript 动态填充 -->
                        </select>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/visitor";
        var manUrl = ctx + "system/man/list";
        $(document).ready(function() {
            loadOldMan();
            $("#form-visitor-add").validate({
                focusCleanup: true
            });

        });
        function loadOldMan() {
            $.ajax({
                url: manUrl,
                type: 'POST',
                dataType: 'json',
                success: function(data) {

                    var $oldManSelect = $('#oldManSelect');
                    console.log($oldManSelect);
                    $oldManSelect.empty(); // 清空现有选项
                    if (data && data.rows && Array.isArray(data.rows)) {
                        data.rows.forEach(function(oldMan) {
                            var option = $('<option></option>')
                                .attr('value', oldMan.id)
                                .text(oldMan.name);
                            $oldManSelect.append(option);
                        });
                    }
                },
                error: function(xhr, status, error) {
                    console.error('加载分类数据失败:', error);
                }
            });
        }
        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-visitor-add').serialize());
            }
        }

        $("input[name='visitTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='visitLeaveTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
</body>
</html>